/*
 * * 
 * Author: Mishal Parmar
 * Date: 28 Oct 2012
 */
package controllers;

import dbhandler.AccessRole;
import dbhandler.ElectionManage;
import dbhandler.EventManage;

import entityclasses.election;
import entityclasses.event;
import entityclasses.roles;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author admin
 */
@WebServlet(name = "modifyElectdata", urlPatterns = {"/modifyElectdata"})
public class modifyElectdata extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException, SQLException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet modifyElectdata</title>");            
            out.println("</head>");
            out.println("<body>");
       
        if(request.getParameter("modify")!=null)
           {
            
               ElectionManage electManage=new ElectionManage();
                EventManage event=new EventManage();
                AccessRole user=new AccessRole();
            
           election elect=new election();
           event evt=new event();
           roles role=new roles();
           roles urole=new roles();
    
            int eid=Integer.parseInt(request.getParameter("electid"));
            
           
            ArrayList electarr=new ArrayList();
            ArrayList evtarr=new ArrayList();
            ArrayList accessuser=new ArrayList();
            ArrayList userrole=new ArrayList();
            
            electarr=electManage.viewelectionbyid(eid);
         elect=(election)electarr.get(0);
     
               out.println(eid);
                       out.println(elect.getEvent_id());
            int eventid=Integer.parseInt(elect.getEvent_id());
        //      out.println("temp4");
            evtarr=event.eventviewbyid(eventid);
             evt=(event)evtarr.get(0);
       
             accessuser=user.accessuserbyid(evt.getAccessId());
             userrole=user.accessuser();
        //                out.println("temp3");
            elect=(election)electarr.get(0);
            evt=(event)evtarr.get(0);
            role=(roles)accessuser.get(0);
            urole=(roles)userrole.get(2);
            
           //          out.println("temp2");
           out.println(" elect " +elect.getElect_id());
           out.println(" access " + evt.getAccessId());
           out.println(" role " + role.getRole_name());
         //  out.println("userrole" + urole.getRole_name());
           
           request.setAttribute("electdata", electarr);
           request.setAttribute("eventdata", evtarr);
           request.setAttribute("accessdata", accessuser);
           request.setAttribute("userroledata", userrole);
           
    RequestDispatcher rd=request.getRequestDispatcher("/eventpage/modifyElectdata.jsp");
    rd.forward(request, response);
           }
           
      else if(request.getParameter("modifydata")!=null)
        {
           // out.println("hello3");
             ElectionManage electManage=new ElectionManage();
           election elect=new election();
           
           EventManage evt=new EventManage();
           
                     
        
        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(dt);
        
        DateFormat formatter ; 
        
        Date date ; 
        formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm");
       
      //  event newevent=new event();
        
        int eventid=Integer.parseInt(request.getParameter("eventid"));  
        int electid=Integer.parseInt(request.getParameter("electid")); 
        int accessid=Integer.parseInt(request.getParameter("accesstype")); 
        
       // newevent.setAccessId(Integer.parseInt(request.getParameter("accesstype")));
       // newevent.setTypeId(Integer.parseInt(request.getParameter("eventtype")));
       // newevent.setPosteddt(currentTime);
                              	        
        
        String[] datetime={request.getParameter("starttime"),         
        request.getParameter("endtime"),
        request.getParameter("nomineestart"),
        request.getParameter("nomineeend")};
        
            for(int i=0;i<4;i++)
        {
            
        date =(Date)formatter.parse(datetime[i]);      
        java.text.SimpleDateFormat datefor = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timedate = datefor.format(date);
        datetime[i]=timedate;
        }
 
        
        elect.setStart_time(datetime[0]);
        elect.setEnd_time(datetime[1]);
        elect.setNominee_start(datetime[2]);
        elect.setNominee_end(datetime[3]);
        
        elect.setElect_id(electid);
     if(electManage.updateElectionquery(electid,elect))
         out.println("Complete Election");
   //  out.println(eventid + " " + accessid);
   if(evt.eventupdatebyid(eventid,accessid))
     out.println("Complete Event");
        //out.println(electid+"  "+elect.getStart_time() + " "+elect.getEnd_time()+" "+elect.getNominee_start() + " "+elect.getNominee_end()+" "+request.getParameter("accesstype"));
            
        }
     
           
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
                    try {
                        processRequest(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(modifyElectdata.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(modifyElectdata.class.getName()).log(Level.SEVERE, null, ex);
                    }
        } catch (ParseException ex) {
            Logger.getLogger(modifyElectdata.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
                    try {
                        processRequest(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(modifyElectdata.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(modifyElectdata.class.getName()).log(Level.SEVERE, null, ex);
                    }
        } catch (ParseException ex) {
            Logger.getLogger(modifyElectdata.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
